package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.SmsNotifications;
import com.example.mall.dto.SmsNotificationsDto;
import com.example.mall.mapper.SmsNotificationsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SmsNotificationsQuery;
import com.example.mall.service.ISmsNotificationsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 短信通知表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SmsNotificationsServiceImpl extends ServiceImpl<SmsNotificationsMapper, SmsNotifications> implements ISmsNotificationsService {

    @Autowired
    private SmsNotificationsMapper smsNotificationsMapper;

    @Override
    public PageDTO<SmsNotificationsDto> queryPage(SmsNotificationsQuery smsNotificationsQuery) {
        if (smsNotificationsQuery == null) {
            smsNotificationsQuery = new SmsNotificationsQuery();
        }
        Page<SmsNotifications> page = smsNotificationsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SmsNotifications> cartPage = lambdaQuery()
                .like(smsNotificationsQuery.getSmsId()!= null, SmsNotifications::getSmsId, smsNotificationsQuery.getSmsId())
                .like(smsNotificationsQuery.getUserId()!= null, SmsNotifications::getUserId, smsNotificationsQuery.getUserId())
                .like(smsNotificationsQuery.getTypeId()!= null, SmsNotifications::getTypeId, smsNotificationsQuery.getTypeId())
                .like(smsNotificationsQuery.getPhoneNumber()!= null, SmsNotifications::getPhoneNumber, smsNotificationsQuery.getPhoneNumber())
                .like(smsNotificationsQuery.getStatus()!= null, SmsNotifications::getStatus, smsNotificationsQuery.getStatus())
                .like(smsNotificationsQuery.getContent()!= null, SmsNotifications::getContent, smsNotificationsQuery.getContent())
                .orderByDesc(SmsNotifications::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SmsNotificationsDto.class);
    }

    @Override
    public SmsNotifications querySmsNotificationsById(Long smsId) {
        AssertUtil.isNotNull(smsId, "smsId不能为空");
        return smsNotificationsMapper.selectById(smsId);
    }

    @Override
    public void insert(SmsNotificationsDto smsNotificationsDto) {
        SmsNotifications smsNotifications = new SmsNotifications();
        CopyUtils.copyProperties(smsNotificationsDto, smsNotifications);
        smsNotifications.setCreateDate(new Date());
        smsNotifications.setUpdateDate(new Date());
        smsNotifications.setStatus(BaseConstants.NOT_USED);
        smsNotifications.setSmsId(CodeGenerateUtils.generateUniqueId());
        smsNotificationsMapper.insert(smsNotifications);
    }

    @Override
    public void update(SmsNotificationsDto smsNotificationsDto) {
        AssertUtil.isNotNull(smsNotificationsDto.getSmsId(), "smsId不能为空");
        SmsNotifications smsNotifications = smsNotificationsMapper.selectById(smsNotificationsDto.getSmsId());
        AssertUtil.isNotNull(smsNotifications, "smsId不存在");
        CopyUtils.copyProperties(smsNotificationsDto, smsNotifications);
        smsNotifications.setUpdateDate(new Date());
        smsNotificationsMapper.updateById(smsNotifications);
    }

    @Override
    public void delete(Long smsId) {
        AssertUtil.isNotNull(smsId, "smsId不能为空");
        SmsNotifications smsNotifications = smsNotificationsMapper.selectById(smsId);
        AssertUtil.isNotNull(smsNotifications, "smsId不存在");
        smsNotifications.setStatus(BaseConstants.EXPIRED);
        smsNotifications.setUpdateDate(new Date());
        smsNotificationsMapper.updateById(smsNotifications);
    }

    @Override
    public void deleteBatch(List<Long> smsIds) {
        AssertUtil.isNotNull(smsIds, "smsIds不能为空");
        List<SmsNotifications> smsNotifications = smsNotificationsMapper.selectBatchIds(smsIds);
        AssertUtil.isNotNull(smsNotifications, "smsIds不存在");
        for (SmsNotifications smsNotification : smsNotifications) {
            smsNotification.setStatus(BaseConstants.EXPIRED);
            smsNotification.setUpdateDate(new Date());
            smsNotificationsMapper.updateById(smsNotification);
        }
    }

    @Override
    public List<SmsNotifications> querySmsNotificationsList() {
        return this.list();
    }
}
